package com.csdn.leon.broadcast;

import java.util.ArrayList;

import com.csdn.leon.activity.MainActivity;
import com.csdn.leon.broadcast.NetBroadcastReceiver.netEventHandler;
import com.csdn.leon.util.TDevice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class ConnectionChangeReceiver extends BroadcastReceiver {
	public static ArrayList<netEventHandler> mListeners =new ArrayList<netEventHandler>();

	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobNetInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiNetInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		if (TDevice.isNetworkAvailable()) {
			 if(mListeners.size()>0){
			    	for(netEventHandler handler:mListeners){
			    		handler.onNetChange();
			    	}
			    }
			
			// 改变背景或者 处理网络的全局变量
		} else {
			Toast.makeText(context, "没有网络", 2000).show();
		}
	}
	
	public static abstract interface netEventHandler{
		public abstract void onNetChange();
	}
}
